# coding=utf-8

import os
from typing import List
from generator.cpp.file_type import CHeader
from generator.cpp.types import CEnum, CStruct
from parse.parse_types import Module
from parse.sdb_parser import ECU


# ServiceId 定义
def gen_ServiceIdHeadFile(ecus: List[ECU], modules: List[Module],outDir:str):
    """
    一个ecu 是一个serviceId
    每个ecu的operid 信号从30开始自动递增 CBPA从100开始由文件定义
    """
    service_id_file = CHeader("ECARX_SIGNAL_SERVICE_ID")
    serviceIdStruct = CStruct("EcarXSignalServiceID")
    rootEnum = CEnum("Root")
    service_id_file.define("MIN_SIGNAL_SERVICE_ID 30")
    service_id_file.define("MAX_SIGNAL_SERVICE_ID 100")

    for ecu in sorted(ecus,key=lambda ecu:ecu.serviceId):
        (name,value) = ecu.getServiceId()
        rootEnum.property(name, value)
    rootEnum.property("serviceID_RX1", "97")
    rootEnum.property("serviceID_RX2", "98")
    max_custom_service_id = 100
    for module in modules:
        if int(module.service_id) > max_custom_service_id:
            max_custom_service_id = int(module.service_id)
        rootEnum.property("serviceID_"+module.name.upper(), module.service_id)
    service_id_file.define("MIN_CUSTOM_SERVICE_ID 100")
    service_id_file.define(
        "MAX_CUSTOM_SERVICE_ID {}".format(max_custom_service_id+1))
    serviceIdStruct.member(rootEnum)
    service_id_file.addMember(serviceIdStruct)

    service_id_file.toFile(os.path.join(outDir,"include/vp"), "SignalServiceId.h")

